package controller.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Datentraeger.DatentraegerInfo;
import model.util.DatentraegerHeader;
import service.DatentraegerService;
import service.util.ServiceException;
import service.util.ServiceFactory;
import controller.util.MessageHolder;
import dao.util.DaoContextEnum;

/**
 * Servlet implementation class DatentraegersServlet
 */
@SuppressWarnings("serial")
public class DatentraegersServlet extends HttpServlet {
	private ArrayList<DatentraegerInfo> info;
	MessageHolder holder = new MessageHolder(); 
	DatentraegerHeader titel = new DatentraegerHeader();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			DatentraegerService service = (DatentraegerService) ServiceFactory.getServiceForName("DatentraegerService", DaoContextEnum.MYSQL);  
			info = service.getDatentraegerInfos();
			request.setAttribute("titel", titel);
			request.setAttribute("info", info);	
			request.getRequestDispatcher("datentraegers.jsp").forward(request, response);
			return;
			
		} catch (ServiceException e){

			holder.setMessage(e.getMessage());
			request.setAttribute("message", holder);
			request.getRequestDispatcher("datentraegers.jsp").forward(request, response);
			return;
		}
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)0
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
